package com.tuanzi.loan.business.repository.xhh.settlementsystem.impl;

import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementCompany;
import com.tuanzi.loan.business.repository.xhh.settlementsystem.custom.XhhSettlementCompanyRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.xhh.settlementsystem.XhhSettlementCompanyRequest;
import com.tuanzi.loan.vo.response.xhh.settlementsystem.XhhSettlementCompanyResponse;
import org.apache.commons.lang3.time.DateUtils;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.Tables.XHH_LOAN_SETTLEMENT_COMPANY;
import static com.tuanzi.loan.generated.jooq.loan.tables.XhhLoanSettlementIncome.XHH_LOAN_SETTLEMENT_INCOME;
import static com.tuanzi.loan.generated.jooq.loan.tables.XhhLoanSettlementPrepay.XHH_LOAN_SETTLEMENT_PREPAY;

/**
 * @author caiwanlin
 * @create 2018-08-16 16:21
 */
public class XhhSettlementCompanyRepositoryImpl extends JOOQBaseRepositoryPlus implements XhhSettlementCompanyRepositoryCustom {
	@Override
	protected TableImpl getTable() {
		return XHH_LOAN_SETTLEMENT_COMPANY;
	}

	@Override
	public Page<XhhSettlementCompanyResponse> list(XhhSettlementCompanyRequest searchRequest, Pageable pageable) {
		Integer billProject = searchRequest.getBillProject();
		String companyName = searchRequest.getCompanyName();
		Integer billType = searchRequest.getBillType();
		String creator = searchRequest.getCreator();
		Date startTime = searchRequest.getStartTime();
		Date endTime = searchRequest.getEndTime();
		if (endTime != null) {
			endTime = DateUtils.addHours(endTime, 23);
			endTime = DateUtils.addMinutes(endTime, 59);
			endTime = DateUtils.addSeconds(endTime, 59);
		}
		fields(XHH_LOAN_SETTLEMENT_COMPANY.fields());
		fields(dslContext.select(DSL.sum(XHH_LOAN_SETTLEMENT_INCOME.INCOME_AMOUNT)).from(XHH_LOAN_SETTLEMENT_INCOME).where(XHH_LOAN_SETTLEMENT_INCOME.COMPANY_ID.eq(XHH_LOAN_SETTLEMENT_COMPANY.ID))
				.asField("INCOME_AMOUNT"));
		fields(dslContext.select(DSL.sum(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_AMOUNT)).from(XHH_LOAN_SETTLEMENT_PREPAY).where(XHH_LOAN_SETTLEMENT_PREPAY.COMPANY_ID.eq(XHH_LOAN_SETTLEMENT_COMPANY.ID))
				.asField("COMPANY_PREPAY_AMOUNT"));
		contains(XHH_LOAN_SETTLEMENT_COMPANY.COMPANY_NAME, companyName);
		eq(XHH_LOAN_SETTLEMENT_COMPANY.BILL_TYPE, billType);
		contains(XHH_LOAN_SETTLEMENT_COMPANY.CREATOR, creator);
		between(XHH_LOAN_SETTLEMENT_COMPANY.CREATE_TIME, startTime, endTime);
		eq(XHH_LOAN_SETTLEMENT_COMPANY.BILL_PROJECT, billProject);
		groupBy(XHH_LOAN_SETTLEMENT_COMPANY.ID);
		orderBy(XHH_LOAN_SETTLEMENT_COMPANY.CREATE_TIME, Sort.Direction.DESC);
		Page<XhhSettlementCompanyResponse> search = search(pageable, record -> {
			XhhSettlementCompanyResponse response = new XhhSettlementCompanyResponse();
			response.setId(record.get(XHH_LOAN_SETTLEMENT_COMPANY.ID));
			response.setCompanyName(record.get(XHH_LOAN_SETTLEMENT_COMPANY.COMPANY_NAME));
			response.setIsBill(record.get(XHH_LOAN_SETTLEMENT_COMPANY.IS_BILL) == null ? 0 : record.get(XHH_LOAN_SETTLEMENT_COMPANY.IS_BILL).intValue());
			response.setBillContent(record.get(XHH_LOAN_SETTLEMENT_COMPANY.BILL_CONTENT));
			response.setTaxNumber(record.get(XHH_LOAN_SETTLEMENT_COMPANY.TAX_NUMBER));
			response.setBillType(record.get(XHH_LOAN_SETTLEMENT_COMPANY.BILL_TYPE));
			response.setBankAddress(record.get(XHH_LOAN_SETTLEMENT_COMPANY.BANK_ADDRESS));
			response.setBankAccount(record.get(XHH_LOAN_SETTLEMENT_COMPANY.BANK_ACCOUNT));
			response.setBankNumber(record.get(XHH_LOAN_SETTLEMENT_COMPANY.BANK_NUMBER));
			response.setReceiveName(record.get(XHH_LOAN_SETTLEMENT_COMPANY.RECEIVE_NAME));
			response.setReceivePhone(record.get(XHH_LOAN_SETTLEMENT_COMPANY.RECEIVE_PHONE));
			response.setReceiveAddress(record.get(XHH_LOAN_SETTLEMENT_COMPANY.RECEIVE_ADDRESS));
			response.setPrepayAmount(record.get("COMPANY_PREPAY_AMOUNT") == null ? (double) 0 : ((BigDecimal) record.get("COMPANY_PREPAY_AMOUNT")).doubleValue());
			response.setExpendAmount(record.get("INCOME_AMOUNT") == null ? (double) 0 : ((BigDecimal) record.get("INCOME_AMOUNT")).doubleValue());
			response.setRemark(record.get(XHH_LOAN_SETTLEMENT_COMPANY.REMARK));
			response.setCreator(record.get(XHH_LOAN_SETTLEMENT_COMPANY.CREATOR));
			response.setCreateTime(record.get(XHH_LOAN_SETTLEMENT_COMPANY.CREATE_TIME));
			response.setUpdator(record.get(XHH_LOAN_SETTLEMENT_COMPANY.UPDATOR));
			response.setUpdateTime(record.get(XHH_LOAN_SETTLEMENT_COMPANY.UPDATE_TIME));
			response.setBillProject(record.get(XHH_LOAN_SETTLEMENT_COMPANY.BILL_PROJECT));
			return response;
		});
		return search;
	}

	@Override
	public List<XhhSettlementCompany> getCompanyList() {
		fields(XHH_LOAN_SETTLEMENT_COMPANY.ID, XHH_LOAN_SETTLEMENT_COMPANY.COMPANY_NAME, XHH_LOAN_SETTLEMENT_COMPANY.BILL_PROJECT);
		List<XhhSettlementCompany> search = search(XhhSettlementCompany.class);
		return search;
	}

	@Override
	public void updateExpendAmount(Long companyId, Double expendAmount) {
		dslContext.update(XHH_LOAN_SETTLEMENT_COMPANY).set(XHH_LOAN_SETTLEMENT_COMPANY.EXPEND_AMOUNT, expendAmount).where(XHH_LOAN_SETTLEMENT_COMPANY.ID.eq(companyId));
	}

	@Override
	public void updatePrepayAmount(Long companyId, Double prepayAmount) {
		dslContext.update(XHH_LOAN_SETTLEMENT_COMPANY).set(XHH_LOAN_SETTLEMENT_COMPANY.PREPAY_AMOUNT, prepayAmount).where(XHH_LOAN_SETTLEMENT_COMPANY.ID.eq(companyId));
	}
}
